package com.pattern.factory.simpleFactory;


import com.pattern.factory.Hotel;
import com.pattern.factory.House;
import com.pattern.factory.NetAboutHouse;
import com.pattern.utils.StringUtils;

/**
 * Description  房屋工厂类
 *
 * @author hubiao
 * @since 2020-08-18 21:18
 */
public class HouseFactory {


    /**
     * 最初版的简单工厂模式
     *
     * @param flag
     * @return
     */
    public House createHouseOne(String flag) {
        if ("hotel".equals( flag )) {
            return new Hotel();
        } else if ("netAbout".equals( flag )) {
            return new NetAboutHouse();
        }
        return null;
    }


    /**
     * 改进第一版：简单工厂模式 + 反射
     *
     * @param className
     * @return
     */
    public House createHouseTwo(String className) {
        try {
            if (StringUtils.isNotEmpty( className )) {
                return (House) Class.forName( className ).newInstance();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 改进第二版：简单工厂模式 + 反射
     *
     * @param className
     * @return
     */
    public House createHouseThree(Class className) {
        try {
            if (className != null) {
                return (House) className.newInstance();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
